package com.zhisheng.controller;

import com.zhisheng.pojo.Dept;
import com.zhisheng.pojo.MyResponseResult;
import com.zhisheng.service.DeptService;
import com.zhisheng.utils.MyThreadLocalUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.List;
import java.util.Map;

/**
 * 部门管理Controller
 */
@RestController
@Slf4j
@RequestMapping("depts")
public class DeptController {

    @Autowired
    private DeptService deptService;

    @GetMapping
    public MyResponseResult<List<Dept>> selectAll(){
        Map<String, Object> value = MyThreadLocalUtils.getValue();
        System.out.println("从ThreadLocal中获取的令牌解析的结果是:================================================================================================"+value);
        // 参  (无参)
        // 调
        List<Dept> depts = deptService.selectAll();
        // 响
        return MyResponseResult.ok(depts);
    }

    @DeleteMapping("/{id}")
    public MyResponseResult<Object> delById(@PathVariable Integer id) throws ParseException {
        // 参 调  响
        int i = deptService.delById(id);
        // 响
        return i>=1?MyResponseResult.ok("ok"):MyResponseResult.error(id+"为的部门删除失败,请确认id后重试!");
    }

    @PostMapping
    public MyResponseResult<Object> saveDept(@RequestBody Dept dept){
        // 参 调  响
        int i = deptService.insertDept(dept);
        return i>=1?MyResponseResult.ok(dept.getId()):MyResponseResult.error("添加部门:"+dept.getName()+" 失败!");
    }

}
